<!DOCTYPE html>
<html>
	<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>SB Admin - Bootstrap Admin Template</title>

    <!-- Bootstrap Core CSS -->
    <link href="__PUBLIC__/home/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <!--<link href="_PUBLIC__/font/css/font-awesome.min.css" rel="stylesheet" type="text/css">-->

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	
</head>
	<body>
		<!--<div class="container-fluid">-->
        <!-- Page Heading -->
	        <div class="row">
				<div class="col-md-12">
					<ol class="breadcrumb">
						<li class="active">
							<i class="fa fa-fw fa-edit"></i>
							命令管理
						</li>
					</ol>
				</div>
			</div>
			<div class="row">
				<ul class="nav nav-tabs" role="tablist">
				    <li role="presentation" class="active"><a href="#message" aria-controls="home" role="tab" data-toggle="tab">基本信息</a></li>
			 	</ul>
			 	<div class="tab-content col-xs-12">
					<div role="tabpanel" class="tab-pane active" id="message">
						<div class="row">
							<div class="col-sm-10">
								<form id="zyn-form-add" class="form-horizontal">
									<div class="form-group" style="display: none;">
										<label for="lc-input-resouse_id" class="control-label col-sm-2">按键</label>
										<div class="col-sm-3">
											<input id="lc-input-resouse_id" class="form-control" type="text" name="resouse_id" value="{$combo.id}"/>
										</div>
										<div class="col-sm-3">
											<p class="lc-p-validate-result p-no-margin" attr-validate = "lc-input-resouse_id">*</p>
										</div>
									</div>
									<div class="form-group">
										<label for="lc-input-key" class="control-label col-sm-2">按键</label>
										<div class="col-sm-3">
											<input id="lc-input-key" class="form-control" type="text" name="key" value="{$combo.key}" disabled="disabled"/>
										</div>
										<div class="col-sm-3">
											<p class="lc-p-validate-result p-no-margin" attr-validate = "lc-select-key">*</p>
										</div>
									</div>
									<div class="form-group">
										<label for="lc-select-command_id" class="control-label col-sm-2">命令</label>
										<div class="col-sm-3">
											<select class="p-no-margin form-control" value="优惠套餐" id="lc-select-command_id">
												<volist name="command" id="com">
													<option value="{$com.command_id}">{$com.command_name}</option>
												</volist>
											</select>
										</div>
										<div class="col-sm-3">
											<p class="lc-p-validate-command p-no-margin" attr-validate = "lc-select-command_id">*</p>
										</div>
									</div>
									<div class="form-group">
										<label for="lc-input-count" class="control-label col-sm-2">循环次数</label>
										<div class="col-sm-3">
											<input id="lc-input-count" class="form-control" type="number" name="count" value="{$combo.count}"/>
										</div>
										<div class="col-sm-3">
											<p class="lc-p-validate-result p-no-margin" attr-validate = "count">*循环次数(-1为无限循环)</p>
										</div>
									</div>
									<div class="form-group">
										<label for="lc-select-choose" class="control-label col-sm-2">选择关联类型</label>
										<div class="col-sm-3">
											<select class="p-no-margin form-control" value="优惠套餐" id="lc-select-choose">
												<option value="1" selected="selected">键值</option>
												<option value="2">条件</option>
											</select>
										</div>
										<div class="col-sm-3">
											<p class="lc-p-validate-command p-no-margin" attr-validate = "lc-select-command_id">*</p>
										</div>
									</div>
									<!--<div id="lc-div-dg" class="selected">-->
										<div class="form-group">
											<label for="lc-input-combo_name" class="control-label col-sm-2">键值</label>
											<div class="col-sm-3">
												<input id="lc-input-combo_name" class="form-control" type="text" name="combo_name" value="{$combo.combo_name}"/>
											</div>
											<div class="col-sm-3">
												<p class="lc-p-validate-result p-no-margin" attr-validate = "combo_name">*</p>
											</div>
										</div>
									<!--</div>-->
									<div id="lc-div-fg" style="display: none;">
										<div style="border: 1px solid" id="lc-div-group0">
											<div class="form-group">
												<label class="control-label col-sm-2">是否添加括号头</label>
												<div class="col-sm-3">
													<select class="p-no-margin lc-select-condition form-control group-value">
														<option value="(">是</option>
														<option value="" selected="selected">否</option>
													</select>
												</div>
											</div>
											<div class="form-group">
												<label for="lc-input-count" class="control-label col-sm-2">条件组合</label>
												<div class="col-sm-3">
													<select class="p-no-margin lc-select-condition form-control group-value select-compare" attr-id="lc-div-control0">
														<volist name="condition" id="con">
															<option value="{$con.condition_value}" attr-id="{$con.compare_id}">{$con.condition_name}</option>
														</volist>
													</select>
												</div>
												<div class="col-sm-3">
													<button class="btn-insert btn btn-primary" type="button">新增条件</button>
												</div>
											</div>
											<div div id="lc-div-control0">
												<div class="form-group">
													<label for="lc-input-count" class="control-label col-sm-2">逻辑类型</label>
													<div class="col-sm-3">
														<select class="p-no-margin lc-select-condition form-control group-value">
															<option value=">" selected="selected">大于</option>
															<option value="<" selected="selected">小于</option>
															<option value="=" selected="selected">等于</option>
														</select>
													</div>
													<div class="col-sm-3">
														<p>*</p>
													</div>
												</div>
												<div class="form-group">
													<label for="lc-input-count" class="control-label col-sm-2">理想值</label>
													<div class="col-sm-3">
														<input class="form-control group-value" type="text" name="count" value="1"/>
													</div>
													<div class="col-sm-3">
														<p class="lc-p-validate-result p-no-margin" attr-validate = "count">*</p>
													</div>
												</div>
											</div>
											
											<div class="form-group">
												<label class="control-label col-sm-2">是否添加括号尾</label>
												<div class="col-sm-3">
													<select class="p-no-margin lc-select-condition form-control group-value">
														<option value=")">是</option>
														<option value="" selected="selected">否</option>
													</select>
												</div>
											</div>
										</div>
									</div>
								</form>
							</div>	
						</div>
					</div>
 				<div class="col-xs-10 col-xs-offset-2">
 					<button class="btn btn-primary" id="xx-btn-save">提交保存</button>
 					<button class="btn btn-primary" id="xx-btn-return">返回上一页</button>
 				</div>
			</div>
		</div>
		
	</body>
	<script src="__PUBLIC__/home/js/jquery.min.js"></script>
    <script src="__PUBLIC__/home/js/bootstrap.min.js"></script>
    <script src=""></script>
	<script>
	var div_count = 0;
    $(document).ready(function() {
    	
    });
    $("#xx-btn-return").click(function(){
    	location.href = "{:U('Combo/index')}";
    });
    $("#xx-btn-save").click(function(){
    	var temp = '';
    	if($("#lc-select-choose").val() == 2){
    		$(".group-value").each(function(){
	    		if($(this).attr('attr-id') == 4){
	    			temp += $(this).find('option[value="'+$(this).val()+'"]').attr('attr-id');
	    		}else{
	    			temp += $(this).val();
	    		}
	    	})
    	}
    	console.log(temp)
		$.ajax({
			type:"post",
			url:"{:U('Combo/reviseComboInfo')}",
			data:{
				'resouse_id': $("#lc-input-resouse_id").val(),
				'command_id': $("#lc-select-command_id").val(),
				'count': $("#lc-input-count").val(),
				'keycondition': $("#lc-select-choose").val(),
				'combo': temp,
				'combo_name': $("#lc-input-combo_name").val(),
			},
			dataType:'json',
			success:function(result){
				//链接服务器其成功
				if(result.status == 1){
					//成功
					return location.href = "{:U('Combo/index')}";
				}else if (result.status == VALIDATE_ERROR){
					//表单验证出错
					var errors = result.msg;
					$('.zyn-p-validate-result').each(function(){
						var errorMsg = errors[$(this).attr('attr-validate')];
						$(this).html(errorMsg != undefined ? '*'+errorMsg : '').css({color:'red'});
					});
				}else{
					return Dialog.error(result.msg);
				}
				
			},
			
		});
    });
    $(".btn-insert").click(function(){
    	div_count += 1;
    	var html = '<div style="border: 1px solid" id="lc-div-group'+div_count+'">'+
    					'<div class="form-group">'+
							'<label class="control-label col-sm-2">条件执行关系类型</label>'+
							'<div class="col-sm-3">'+
								'<select class="p-no-margin lc-select-condition form-control group-value">'+
									'<option value="and" selected="selected">and</option>'+
									'<option value="or">or</option>'+
								'</select>'+
							'</div>'+
						'</div>'+
						'<div class="form-group">'+
							'<label class="control-label col-sm-2">是否添加括号头</label>'+
							'<div class="col-sm-3">'+
								'<select class="p-no-margin lc-select-condition form-control group-value">'+
									'<option value="(">是</option>'+
									'<option value="" selected="selected">否</option>'+
								'</select>'+
							'</div>'+
						'</div>'+
						'<div class="form-group">'+
							'<label for="lc-input-count" class="control-label col-sm-2">条件组合</label>'+
							'<div class="col-sm-3">'+
								'<select class="p-no-margin lc-select-condition form-control group-value select-compare" attr-id="lc-div-control'+div_count+'">'+
									'<volist name="condition" id="con">'+
										'<option value="{$con.condition_value}" attr-id="{$con.compare_id}">{$con.condition_name}</option>'+
									'</volist>'+
								'</select>'+
							'</div>'+
							'<div class="col-sm-3">'+
								'<button class="btn-remove btn btn-primary" data-id="'+div_count+'" type="button" onclick="checkRemove(this)">删除条件</button>'+
							'</div>'+
						'</div>'+
						'<div id="lc-div-control'+div_count+'">'+
							'<div class="form-group">'+
								'<label for="lc-input-count" class="control-label col-sm-2">逻辑类型</label>'+
								'<div class="col-sm-3">'+
									'<select class="p-no-margin lc-select-condition form-control group-value">'+
										'<option value=">" selected="selected">大于</option>'+
										'<option value="<" selected="selected">小于</option>'+
										'<option value="=" selected="selected">等于</option>'+
									'</select>'+
								'</div>'+
								'<div class="col-sm-3">'+
									'<p>*</p>'+
								'</div>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="lc-input-count" class="control-label col-sm-2">理想值</label>'+
								'<div class="col-sm-3">'+
									'<input class="form-control group-value" type="text" name="count" value="1"/>'+
								'</div>'+
								'<div class="col-sm-3">'+
									'<p class="lc-p-validate-result p-no-margin" attr-validate = "count">*</p>'+
								'</div>'+
							'</div>'+
						'</div>'+
						'<div class="form-group">'+
							'<label class="control-label col-sm-2">是否添加括号尾</label>'+
							'<div class="col-sm-3">'+
								'<select class="p-no-margin lc-select-condition form-control group-value">'+
									'<option value=")">是</option>'+
									'<option value="" selected="selected">否</option>'+
								'</select>'+
							'</div>'+
						'</div>'+
					'</div>'
    	$("#lc-div-fg").append(html);
    });
    function checkRemove(obj){
    	var temp =$(obj).attr('data-id');
    	$("#lc-div-group"+temp).remove();  
    }
    $("#lc-select-choose").on('change',function(){
    	if($("#lc-select-choose").val() == 1){
    		$("#lc-div-dg").addClass('selected');
    		$("#lc-div-fg").removeClass('selected');
    		$("#lc-div-dg").css('display','block');
    		$("#lc-div-fg").css('display','none');
    	}else if($("#lc-select-choose").val() == 2){
    		$("#lc-div-fg").addClass('selected');
    		$("#lc-div-dg").removeClass('selected');
    		$("#lc-div-fg").css('display','block');
    		$("#lc-div-dg").css('display','none');
    	}
    });
    $(document).on('change','.select-compare',function(){
//  	$(".select-compare").on('change',function(){
    	var val = $(this).val();
    	var temp_control = $(this).attr('attr-id');
    	var id = $(this).find('option[value="'+val+'"]').attr('attr-id');
    	var html = '';
    	$.ajax({
			type:"post",
			url:"{:U('Combo/getConditionInfo')}",
			data:{
				'condition_id' : id
			},
			dataType:'json',
			success:function(result){
				console.log(result.data.condition.compare_id)
				if(result.data.condition.compare_id == 2){
					html = '<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">逻辑类型</label>'+
						'<div class="col-sm-3">'+
							'<select class="p-no-margin lc-select-condition form-control group-value">'+
								'<option value="=" selected="selected">等于</option>'+
							'</select>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p>*</p>'+
						'</div>'+
					'</div>'+
					'<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">理想值</label>'+
						'<div class="col-sm-3">'+
							'<input class="form-control group-value" type="text" name="count" value="1"/>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p class="lc-p-validate-result p-no-margin group-value" attr-validate = "count">'+result.data.condition.unit+'</p>'+
						'</div>'+
					'</div>';
				}else if(result.data.condition.compare_id == 3){
					html = '<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">逻辑类型</label>'+
						'<div class="col-sm-3">'+
							'<select class="p-no-margin lc-select-condition form-control group-value">'+
								'<option value="=" selected="selected">等于</option>'+
							'</select>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p>*</p>'+
						'</div>'+
					'</div>'+
					'<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">理想值</label>'+
						'<div class="col-sm-3">'+
							'<input class="form-control group-value" type="text" name="count" value="1"/>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p class="lc-p-validate-result p-no-margin group-value" attr-validate = "count">'+result.data.condition.unit+'</p>'+
						'</div>'+
					'</div>'+
					'<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">定时时间</label>'+
						'<div class="col-sm-3">'+
							'<input class="form-control group-value" type="text" name="count" value="1"/>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p class="lc-p-validate-result p-no-margin" attr-validate = "count">分钟</p>'+
						'</div>'+
					'</div>';
					;
				
				}else if(result.data.condition.compare_id == 4 || result.data.condition.compare_id == 5){
					html = '<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">逻辑类型</label>'+
						'<div class="col-sm-3">'+
							'<select class="p-no-margin lc-select-condition form-control group-value">'+
								'<option value="=" selected="selected">等于</option>'+
							'</select>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p>*</p>'+
						'</div>'+
					'</div>'+
					'<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">理想值</label>'+
						'<div class="col-sm-3">'+
							'<select class="form-control group-value" name="count" attr-id = '+result.data.condition.compare_id+'>'+
							'<option attr-id="-1" value="所有人">所有人</option>';
							console.log(result.data.user)
						for(var i in result.data.user){
							html += '<option value="'+result.data.user[i].username+'" attr-id='+result.data.user[i].pkid+'>'+result.data.user[i].username+'</option>';
						}
							html += '</select>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p class="lc-p-validate-result p-no-margin group-value" attr-validate = "count">'+result.data.condition.unit+'</p>'+
						'</div>'+
					'</div>';
				}else{
					html = '<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">逻辑类型</label>'+
						'<div class="col-sm-3">'+
							'<select class="p-no-margin lc-select-condition form-control group-value">'+
								'<option value=">" selected="selected">大于</option>'+
								'<option value="<" selected="selected">小于</option>'+
								'<option value="=" selected="selected">等于</option>'+
							'</select>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p>*</p>'+
						'</div>'+
					'</div>'+
					'<div class="form-group">'+
						'<label for="lc-input-count" class="control-label col-sm-2">理想值</label>'+
						'<div class="col-sm-3">'+
							'<input class="form-control group-value" type="text" name="count" value="1"/>'+
						'</div>'+
						'<div class="col-sm-3">'+
							'<p class="lc-p-validate-result p-no-margin group-value" attr-validate = "count">'+result.data.condition.unit+'</p>'+
						'</div>'+
					'</div>';
				
				}
		    	$("#"+temp_control).empty();
		    	console.log(id)
		    	console.log(temp_control)
		    	console.log(html)
		    	$("#"+temp_control).html(html);
			}
			
		});
    	
    });
	</script>
</html>
